package cn.com.hbhgkj.web.struts2.admin;

import java.util.List;

import org.apache.struts2.json.annotations.JSON;

import cn.com.hbhgkj.bussiness.CopyrightAlterationService;
import cn.com.hbhgkj.bussiness.CopyrightApplicationService;
import cn.com.hbhgkj.bussiness.CopyrightRegisterationService;
import cn.com.hbhgkj.bussiness.UserService;
import cn.com.hbhgkj.bussiness.exception.UserNotValidException;
import cn.com.hbhgkj.common.ServiceException;
import cn.com.hbhgkj.pojo.CopyrightAlteration;
import cn.com.hbhgkj.pojo.CopyrightApplication;
import cn.com.hbhgkj.pojo.CopyrightRegisteration;
import cn.com.hbhgkj.pojo.User;
import cn.com.hbhgkj.web.struts2.BaseAction;

public class ReviewAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String reviewApplication() {
		try {
			copyrightApplicationService.review(this.getLoggedInUser(),
					copyrightApplication);

			this.success = true;
			this.addActionMessage("审核成功，请通过菜单查看历史审核记录!");
		} catch (ServiceException e) {
			this.success = false;
			this.addActionMessage("审核失败，服务器异常！");
			return "review_failed";
		}
		return "review_success";
	}

	public String reviewApplicant() {
		try {
			userService.reviewRegisteration(this.getLoggedInUser(), user);
			this.success = true;
		} catch (UserNotValidException e) {
			this.success = false;
			this.message = e.getMessage();
		} catch (ServiceException e) {
			this.success = false;
			this.message = "审核失败，服务器异常！";
		}
		return SUCCESS;
	}

	public String reviewAlteration() {
		try {
			copyrightAlterationService.review(this.getLoggedInUser(),
					copyrightAlteration);
			this.success = true;
			this.addActionMessage("审核成功，请通过菜单查看历史审核记录!");
		} catch (ServiceException e) {
			this.success = false;
			this.addActionMessage("审核失败，服务器异常！");
			return "review_failed";
		}
		return "review_success";
	}

	public String applicantsPendingReview() {
		try {
			listUser = userService.getPendingReviews(pagination);
			this.success = true;
		} catch (ServiceException e) {
			e.printStackTrace();
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
	}

	public String alterationsPendingReview() {
		try {
			listCopyrightAlteration = copyrightAlterationService
					.getPendingReviews(pagination);
			this.success = true;
		} catch (ServiceException e) {
			e.printStackTrace();
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
	}

	public String applicationsPendingReview() {
		try {
			listCopyrightApplication = copyrightApplicationService
					.getPendingReviews(pagination);
			this.success = true;
		} catch (ServiceException e) {
			e.printStackTrace();
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
	}

	public String reviewedApplications() {
		try {
			listCopyrightApplication = copyrightApplicationService
					.getReviwed(pagination);
			this.success = true;
		} catch (ServiceException e) {
			e.printStackTrace();
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
	}

	public String reviewedAlterations() {
		try {
			listCopyrightAlteration = copyrightAlterationService
					.getReviwed(pagination);
			this.success = true;
		} catch (ServiceException e) {
			e.printStackTrace();
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
	}

	public String reviewedApplicants() {
		try {
			listUser = userService.getReviwed(pagination);
			this.success = true;
		} catch (ServiceException e) {
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
	}

	public String copyrightRegs() {
		try {
			listCopyrightRegisteration = copyrightRegisterationService
					.getAll(pagination);
			this.success = true;
		} catch (ServiceException e) {
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
	}

	public String clearPassword() {
		try {
			userService.clearPassword(this.getLoggedInUser(), user);
			this.success = true;
		} catch (ServiceException e) {
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;

	}

	public String userSearch() {
		try {
			listUser = userService.findLikeCompanyName(user.getCompanyName(),
					pagination);
			this.success = true;
		} catch (ServiceException e) {
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;

	}

	public String regSearch() {
		try {
			listCopyrightRegisteration = copyrightRegisterationService
					.findLikeCopyrighter(
							copyrightRegisteration.getCopyrighter(), pagination);
			this.success = true;
		} catch (ServiceException e) {
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;

	}

	public String syncRegistData() {
		try {
			copyrightRegisterationService.syncFromCopyright();
			this.success = true;
		} catch (Exception e) {
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
	}
	
	
	public String updateCopyrightRegist(){
		try {
			copyrightRegisterationService.updateCopyrightReg(copyrightRegisteration);
			this.success = true;
		} catch (Exception e) {
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
	}

	private int reviewCode;

	private CopyrightRegisteration copyrightRegisteration;

	public CopyrightRegisteration getCopyrightRegisteration() {
		return copyrightRegisteration;
	}

	public void setCopyrightRegisteration(
			CopyrightRegisteration copyrightRegisteration) {
		this.copyrightRegisteration = copyrightRegisteration;
	}

	private List<CopyrightRegisteration> listCopyrightRegisteration;

	public List<CopyrightRegisteration> getListCopyrightRegisteration() {
		return listCopyrightRegisteration;
	}

	public void setListCopyrightRegisteration(
			List<CopyrightRegisteration> listCopyrightRegisteration) {
		this.listCopyrightRegisteration = listCopyrightRegisteration;
	}

	public int getReviewCode() {
		return reviewCode;
	}

	public void setReviewCode(int reviewCode) {
		this.reviewCode = reviewCode;
	}

	private CopyrightApplication copyrightApplication;
	private CopyrightAlteration copyrightAlteration;
	private User user;
	private List<CopyrightApplication> listCopyrightApplication;
	private List<CopyrightAlteration> listCopyrightAlteration;

	public CopyrightApplication getCopyrightApplication() {
		return copyrightApplication;
	}

	public void setCopyrightApplication(
			CopyrightApplication copyrightApplication) {
		this.copyrightApplication = copyrightApplication;
	}

	public CopyrightAlteration getCopyrightAlteration() {
		return copyrightAlteration;
	}

	public void setCopyrightAlteration(CopyrightAlteration copyrightAlteration) {
		this.copyrightAlteration = copyrightAlteration;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<CopyrightApplication> getListCopyrightApplication() {
		return listCopyrightApplication;
	}

	public void setListCopyrightApplication(
			List<CopyrightApplication> listCopyrightApplication) {
		this.listCopyrightApplication = listCopyrightApplication;
	}

	public List<CopyrightAlteration> getListCopyrightAlteration() {
		return listCopyrightAlteration;
	}

	public void setListCopyrightAlteration(
			List<CopyrightAlteration> listCopyrightAlteration) {
		this.listCopyrightAlteration = listCopyrightAlteration;
	}

	public List<User> getListUser() {
		return listUser;
	}

	public void setListUser(List<User> listUser) {
		this.listUser = listUser;
	}

	private List<User> listUser;

	private CopyrightApplicationService copyrightApplicationService;
	private CopyrightAlterationService copyrightAlterationService;

	private CopyrightRegisterationService copyrightRegisterationService;

	@JSON(serialize = false)
	public CopyrightRegisterationService getCopyrightRegisterationService() {
		return copyrightRegisterationService;
	}

	public void setCopyrightRegisterationService(
			CopyrightRegisterationService copyrightRegisterationService) {
		this.copyrightRegisterationService = copyrightRegisterationService;
	}

	private UserService userService;

	@JSON(serialize = false)
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@JSON(serialize = false)
	public CopyrightAlterationService getCopyrightAlterationService() {
		return copyrightAlterationService;
	}

	public void setCopyrightAlterationService(
			CopyrightAlterationService copyrightAlterationService) {
		this.copyrightAlterationService = copyrightAlterationService;
	}

	@JSON(serialize = false)
	public CopyrightApplicationService getCopyrightApplicationService() {
		return copyrightApplicationService;
	}

	public void setCopyrightApplicationService(
			CopyrightApplicationService copyrightApplicationService) {
		this.copyrightApplicationService = copyrightApplicationService;
	}
}
